package com.parking.carsystem.parkingchargesystem.view.time;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;


import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.module.AbsenceData;

import java.util.ArrayList;

import ttyy.com.recyclerexts.base.EXTRecyclerAdapter;
import ttyy.com.recyclerexts.base.EXTViewHolder;


public class BottomDialog extends Dialog {
    View view;
    private RecyclerView event_text_recycle;
    private EXTRecyclerAdapter<AbsenceData> eventAdapter;
    private ChooseTextLisenter chooseTextLisenter;

    public ChooseTextLisenter getChooseTextLisenter() {
        return chooseTextLisenter;
    }

    public void setChooseTextLisenter(ChooseTextLisenter chooseTextLisenter) {
        this.chooseTextLisenter = chooseTextLisenter;
    }

    public BottomDialog(@NonNull Context context) {
        super(context);
        init();
    }

    public BottomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    protected BottomDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init();
    }

    /**
     * 初始化一些参数
     */
    private void init() {
        view = View.inflate(this.getContext(), R.layout.item_bottom_dialog, null);
        event_text_recycle = (RecyclerView) view.findViewById(R.id.event_text_recycle);
        View cancle = view.findViewById(R.id.text_dialog);
        setContentView(view);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        //1,添加这一行代码 dialog 会自动测量高度  根据你设置的背景测量高度 不添加的话,显示不出来  或者在 xml中添加背景,这种设置的recycleView不能滑动,2, 或者直接设置高度 不设置Drawble背景
        //直接给高度, 设置背景为color颜色,但是这个显示不太好看 这种的话 recycleView 可以滑动
//        dialog.getWindow().setBackgroundDrawable(new DrawableWrapper(getResources().getDrawable(R.drawable.dialog_event)));
        getWindow().setBackgroundDrawable(new ColorDrawable());
        getWindow().setAttributes(params);
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击取消按钮
                dismiss();
            }
        });
        event_text_recycle.setLayoutManager(new LinearLayoutManager(getContext()));
        eventAdapter = new EXTRecyclerAdapter<AbsenceData>(R.layout.item_dialog_text) {
            @Override
            public void onBindViewHolder(EXTViewHolder extViewHolder, int i, AbsenceData bean) {
                extViewHolder.setText(R.id.text_dialog, bean.text);
                if (bean.choose) {
                    extViewHolder.setTextColor(R.id.text_dialog, R.color.cal_color_red);
                } else {
                    extViewHolder.setTextColor(R.id.text_dialog, R.color.text_blue);
                }
                if (eventAdapter.getDatas().size() - 1 == i) {
                    extViewHolder.setVisibility(R.id.line_dialog, View.GONE);
                } else {
                    extViewHolder.setVisibility(R.id.line_dialog, View.VISIBLE);
                }
            }
        };
        eventAdapter.setDatas(null);
        eventAdapter.setOnItemClickListener(new EXTRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClicked(View view, int i) {
                if (chooseTextLisenter != null) {
                    chooseTextLisenter.chooseText(eventAdapter.getDatas().get(i));
                }
//                edt_content.setText(edt_content.getText().toString().trim() + eventRemarksResult.get(i).dataName);
                dismiss();
            }
        });
        event_text_recycle.setAdapter(eventAdapter);
    }

    public void setBottomData(ArrayList<AbsenceData> bottomData) {
        eventAdapter.setDatas(bottomData);
        eventAdapter.notifyDataSetChanged();
    }

    public interface ChooseTextLisenter {
        void chooseText(AbsenceData text);
    }
}
